package cn.libinal.scloud.base.service;

import cn.libinal.scloud.base.common.service.BaseJpaService;
import cn.libinal.scloud.base.entity.QUser;
import cn.libinal.scloud.base.entity.User;
import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.StringExpression;
import org.springframework.data.querydsl.binding.QuerydslBinderCustomizer;
import org.springframework.data.querydsl.binding.QuerydslBindings;

import java.util.List;

/**
 * 用户接口
 *
 * @author libinal
 * created at 2018/11/4 16:05
 */
public interface UserService extends BaseJpaService<User>, QuerydslBinderCustomizer<QUser> {

    /**
     * 关联角色
     * @param userId 用户id
     * @param roleIds 角色id
     */
    void relateRoles(String userId, List<String> roleIds);

    /**
     * 构建分页查询条件
     * @param bindings QuerydslBindings
     * @param root QUser
     */
    @Override
    default void customize(QuerydslBindings bindings, QUser root) {
        bindings.bind(root.username).first(StringExpression::contains);
        bindings.bind(root.enabled).first(BooleanExpression::eq);
    }

    User findByUsername(String username);
}
